﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EmaMetadata;
using System.IO;

namespace MetadataTest
{
    [TestClass]
    public class UnitTest1
    {
        /// <summary>
        /// This test confirms that the EMA project was able to successfully load a manually typed file correctly.  
        /// All values used in the test were written by Ted as part of the EMA project.
        /// </summary>
        [TestMethod]
        public void TedsFileUnitTest()
        {
            // Read in the test file from ted's sample data folder
            EmaBatch b = Factory.LoadBatchFromString(Resource1.schema_template);

            // File should have one product
            Assert.AreEqual(b.Products.Count, 1);

            // Okay, let's examine this product
            EmaProduct p = b.Products[0];

            // Verify that its identities loaded correctly
            Assert.AreEqual(p.GetIdentity("org.gs1", "GTIN-14").Value, "00014633098891"); 
            Assert.AreEqual(p.GetIdentity("com.eedar", "EEDAR").Value, "219511");
            Assert.AreEqual(p.GetIdentity("com.microsoft", "GUID").Value, "{0002E101-0000-0000-C000-000000000046}");

            // Verify that the complete title is correct
            Assert.AreEqual(p.GetTitle("org.entmerch", "Complete").Value, "Uncharted 2: Among Thieves");

            // Verify a couple hand selected facts
            Assert.AreEqual(p.EditionType.FactName, "Platinum");

            // Verify a couple hand selected facts
            Assert.AreEqual(p.PrimaryDeveloper, "Primary Developer");
        }

        [TestMethod]
        public void SaveAndLoadTest()
        {
            // Read in the test file from ted's sample data folder
            EmaBatch b = Factory.LoadBatchFromString(Resource1.schema_template);

            // Save this record to a string
            string xml = Factory.SaveBatchToString(b);

            // Reload the string back into a batch
            EmaBatch reloaded = Factory.LoadBatchFromString(xml);
        }

        [TestMethod]
        public void TestGetAndSetDesc()
        {
            EmaProduct product = new EmaProduct();
            product.SetDescription("org.entmerch", "short", 1, "Hi");
            Assert.AreEqual("Hi", product.GetDescription("org.entmerch", "short").Value);
        }

        [TestMethod]
        public void TestExtraSection()
        {
            // Generate a product with some extra data
            EmaProduct p = new EmaProduct();
            p.Extra = "<parent><Node>Hi</Node></parent>";
            string s = Factory.ProductToString(p);

            // Reload and save the product - see if extra doubles itself!
            EmaProduct p2 = Factory.StringToProduct(s);
            Assert.AreEqual(p.Extra, p2.Extra);
        }

        [TestMethod]
        public void TestISOCodes()
        {
            Assert.AreEqual(Factory.ISOCountries["US"], "United States");
        }

        [TestMethod]
        public void TestPriceSetting()
        {
            EmaProduct p = new EmaProduct();

            // Try the same thing with a nullable
            Nullable<DateTime> ndt = null;

            // Attempt to set a "null" pricing time period
            p.SetPrice(Factory.ENTMERCH_NAMESPACE, "MSRP", Factory.PRICE_MSRP, "US", "USD", 59.99m, ndt);
            EmaPrice pr = p.GetPrice(Factory.ENTMERCH_NAMESPACE, "MSRP", Factory.PRICE_MSRP, "US", ndt);
            Assert.IsNotNull(pr);

            // Attempt to set a "non-null" pricing time period
            ndt = new DateTime(1999, 1, 1);
            p.SetPrice(Factory.ENTMERCH_NAMESPACE, "MSRP", Factory.PRICE_MSRP, "US", "USD", 59.99m, ndt);
            pr = p.GetPrice(Factory.ENTMERCH_NAMESPACE, "MSRP", Factory.PRICE_MSRP, "US", new DateTime(1999, 1, 1));
            Assert.IsNotNull(pr);

            // Test slightly different date settings
            ndt = DateTime.Now;
            p.SetPrice(Factory.ENTMERCH_NAMESPACE, "MSRP", Factory.PRICE_MSRP, "US", "USD", 59.99m, ndt);
            pr = p.GetPrice(Factory.ENTMERCH_NAMESPACE, "MSRP", Factory.PRICE_MSRP, "US", new DateTime(ndt.Value.Year, ndt.Value.Month, ndt.Value.Day));
            Assert.IsNotNull(pr);

            // At this point we should have two dates in the system
            Assert.AreEqual(3, p.Pricing.Count);

            // Remove one of the prices
            p.RemovePrice(Factory.ENTMERCH_NAMESPACE, "MSRP", Factory.PRICE_MSRP, "US", ndt);
            Assert.AreEqual(2, p.Pricing.Count);
            pr = p.GetPrice(Factory.ENTMERCH_NAMESPACE, "MSRP", Factory.PRICE_MSRP, "US", ndt);
            Assert.IsNull(pr);
            pr = p.GetPrice(Factory.ENTMERCH_NAMESPACE, "MSRP", Factory.PRICE_MSRP, "US", null);
            Assert.IsNotNull(pr);
        }

        [TestMethod]
        public void TestHtmlEntities()
        {
            EmaProduct p = new EmaProduct();
            p.SetDescription(Factory.ENTMERCH_NAMESPACE, "Long Description", 1, @"Dishonored is an immersive first-person action game that casts you as a supernatural assassin driven by revenge. Creatively eliminate your targets with the flexible combat system as you combine the numerous supernatural abilities, weapons and unusual gadgets at your disposal. Dishonored is set in Dunwall, an industrial whaling city where strange technology and otherworldly mysticism coexist in the shadows. You are the once-trusted bodyguard of the beloved Empress. Framed for her murder, you become an infamous assassin, known only by the disturbing mask that has become your calling card. In a time of uncertainty, when the city is being besieged by plague and ruled by an oppressive government armed with neo-industrial technologies, dark forces conspire to bestow upon you abilities beyond those of any common man  but at what cost? The truth behind your betrayal is as murky as the waters surrounding the city, and the life you once had is gone forever.
Improvise and Innovate: Approach each assassination with your own style of play. Use shadow and sound to your advantage to make your way silently through levels unseen by foes, or attack enemies head-on as they respond to your aggressiveness. The flexible combat system allows you to creatively combine your abilities, supernatural powers and gadgets as you make your way through the levels and dispatch your targets. Improvise and innovate to define your play style.
Action with Meaning: The world of Dishonored reacts to how you play. Move like a ghost and resist corruption, or show no mercy and leave a path of destruction in your wake. Decide your approach for each mission, and the outcomes will change as a result.
Supernatural Abilities: Teleport for stealth approaches, possess any living creature, or stop time itself to orchestrate unearthly executions! Combining your suite of supernatural abilities and weapons opens up even more ways to overcome obstacles and eliminate targets. The games upgrade system allows for the mastery of deadly new abilities and devious gadgets.
A City Unlike Any Other: Enter an original world envisioned by Half-Life 2 art director Viktor Antonov. Arkane and Bethesda bring you a world where industry and mysticism collide, creating an atmosphere thick with intrigue. The world is yours to discover.
ESRB Rating: MATURE with Blood and Gore, Strong Language, Sexual Themes, and Intense Violence");
            string s = Factory.ProductToString(p);
            EmaProduct p2 = Factory.StringToProduct(s);
        }
    }
}
